/**
 * 
 */
package com.lehecai.core.api.user;

import com.lehecai.core.YesNoStatus;
import com.lehecai.core.api.AbstractApiResultBean;
import com.lehecai.core.lottery.*;
import com.lehecai.core.util.CoreDateUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @author Sunshow
 * 提款日志
 */
public class WithdrawLog extends AbstractApiResultBean {
	public static final String QUERY_ID = "withdraw_id";
	public static final String QUERY_IDS = "withdraw_ids";
	public static final String QUERY_USERNAME = "username";
	public static final String QUERY_UID = "uid";
	public static final String QUERY_AMOUNT = "amount";
	public static final String QUERY_TIMELINE = "timeline";
	public static final String QUERY_STATUS = "status";
	public static final String QUERY_PHONE = "phone";
	public static final String QUERY_ID_DATA = "id_data";
	public static final String QUERY_BANK_ID = "bank_id";
	public static final String QUERY_BANK_TYPE = "bank_type";
	public static final String QUERY_BANK_REALNAME = "bank_realname";
	public static final String QUERY_BANK_BRANCH = "bank_branch";
	public static final String QUERY_BANK_CARDNO = "bank_cardno";
	public static final String QUERY_WITHDRAW_TYPE = "type";
	public static final String QUERY_IS_EXPORT = "is_export";
	public static final String QUERY_BATCH_NO = "batch_no";
	public static final String QUERY_WALLET_TYPE = "wallet_type";
	public static final String QUERY_FEE = "fee";
	public static final String QUERY_DESCRIPTION = "description";
	public static final String QUERY_SUCCESS_TIME = "successtime";
    public static final String QUERY_REAL_FEE_STATUS = "real_fee_status";
    public static final String QUERY_REAL_FEE = "real_fee";
    public static final String QUERY_REMIT_CHANNAL = "remit_channel";
	public static final String QUERY_REFUND_TYPE = "refund_type";

	public static final String SERIALNUMBER = "serial";
	public static final String ACQSSN = "acqssn";
	public static final String TRANSDATETIME = "trans_datetime";

	public static final String ORDER_LOG_ID = "withdraw_id";
	public static final String ORDER_AMOUNT = "amount";
	public static final String ORDER_TIMELINE = "timeline";
	public static final String ORDER_SUCCESS_TIME = "successtime";
	
	public static final String SET_WITHDRAW_REMARK = "remark";
	public static final String SET_DELAY_DAY = "day";
	public static final String SET_IS_EXPORT = "is_export";
	public static final String SET_BATCH_NO = "batch_no";
	public static final String SET_PROVINCE = "province";
	public static final String SET_CITY = "city";
	public static final String SET_DESCRIPTION = "description";
	public static final String SET_REAL_FEE = "real_fee";

	
	public static final String EXSIT_AMOUNT_TOTAL = "exsitAmountTotal";
	public static final String MATCH_AMOUNT_TOTAL = "matchAmountTotal";


	
	private String id;			//流水ID
	private long uid;			//用户ID
	private String username;	//用户名
	
	private Date timeline;		//创建时间
	private double amount;		//提款金额
	private Double fee;			//提款手续费
	
	private WithdrawStatus withdrawStatus;	//提款状态
	
	private String phone;
	private String idData;			//证件号码
	
	private BankType bankType;		//银行
	private String bankId;			//银行id
	private String bankRealname;	//提款用户真实姓名
	private String bankBranch;		//银行分行
	private String bankCardno;		//银行卡号
	
	private WithdrawType withdrawType;	//提款类型
	
	private YesNoStatus exportStatus;
	
	private String frozenId;		//冻结ID
	
	private String extra;			//推迟时间
	
	private String remark;			//备注信息
	
	private String batchNo;			//导出批次号
	
	private int provinceId;			//省ID
	private String provinceName;	//省NAME
	
	private int cityId;				//市ID
	private String cityName;		//市NAME
	private WalletType walletType;	//钱包类型
	
	private String description;		//用户查询信息
	private Date successTime;		//汇款、退款成功时间
	
	private String bankName;		        //银行名称-支付宝操作成功与否
	private String withdrawSerialNumber;	//提现流水号-支付宝操作成功与否
	private String memberSerialNumber;	    //商户流水号-支付宝操作成功与否
	private YesNoStatus resultStatus;	    //成功失败标志-支付宝操作成功与否
	private YesNoStatus remitReturnStatus;	//是否退票-支付宝操作成功与否
	private Integer resultStatusValue;      //成功失败标志-支付宝操作成功与否-值

    private String acqssn;//银联语音退款查询必须的追踪号，根据退款是返回的数据更新到ext字段
    private String transDatetime;//银联语音退款查询必须的申请时间，根据退款是返回的数据更新到ext字段

	private boolean confirmFlag;//确认是否匹配标识
	private String exsitValue;//数据库中存在的数据
	private String importValue;//excel中导入的数据
	private String comfirmMemo;//确认时备注


	private RemitChannel remitChannel;//提款记录渠道
	private double realFee;//公司提供手续费
	private YesNoStatus realFeeStatus;//公司提供手续费状态
	private WithdrawRefundManualType refundType;//标志: 提款、退款、特殊退款

	public static WithdrawLog convertFromJSONObject(JSONObject object) {
		if (object == null) {
			return null;
		}
		WithdrawLog log = new WithdrawLog();
		int walletTypeValue = getInt(object, "wallet_type");
		log.walletType = WalletType.getItem(walletTypeValue);
		
		log.id = getString(object, "withdraw_id");
		log.uid = getLong(object, "uid");
		log.username = getString(object, "username");
		
		log.provinceName = getString(object, "province_name");
		log.cityName = getString(object, "city_name");
		
		log.provinceId = getInt(object, "province");
		log.cityId = getInt(object, "city");
		
		log.timeline = CoreDateUtils.parseDate(getString(object, "timeline"), CoreDateUtils.DATETIME);
		log.amount = getDouble(object, "amount");
		
		int iWithdrawStatus = getInt(object, "status");
		if (iWithdrawStatus >= 0) {
			log.withdrawStatus = WithdrawStatus.getItem(iWithdrawStatus);
		}
		
		log.phone = getString(object, "phone");
		log.idData = getString(object, "id_data");
		
		int iBankType = getInt(object, "bank_type");
		if (iBankType >= 0) {
			log.bankType = BankType.getItem(iBankType);
		}
		
		log.bankRealname = getString(object, "bank_id");
		log.bankRealname = getString(object, "bank_realname");
		log.bankBranch = getString(object, "bank_branch");
		log.bankCardno = getString(object, "bank_cardno");
		
		int iWithdrawType = getInt(object, "type");
		if (iWithdrawType >= 0) {
			log.withdrawType = WithdrawType.getItem(iWithdrawType);
		}
		
		log.exportStatus = YesNoStatus.getItem(getInt(object, "is_export"));
		
		log.frozenId = getString(object, "frozen_id");
		String extra = getString(object, "ext");
		log.extra = extra;
		if (extra != null && extra.indexOf(SERIALNUMBER) >= 0) {
			JSONObject extraObj = JSONObject.fromObject(extra);
			if (extraObj.containsKey(SERIALNUMBER)) {
				log.withdrawSerialNumber = getString(extraObj, SERIALNUMBER);
			}
		}
        if (extra != null && extra.indexOf(ACQSSN) >= 0) {
            JSONObject extraObj = JSONObject.fromObject(extra);
            if (extraObj.containsKey(ACQSSN)) {
                log.acqssn = getString(extraObj, ACQSSN);
            }
        }
        if (extra != null && extra.indexOf(TRANSDATETIME) >= 0) {
            JSONObject extraObj = JSONObject.fromObject(extra);
            if (extraObj.containsKey(TRANSDATETIME)) {
                log.transDatetime = getString(extraObj, TRANSDATETIME);
            }
        }
		log.remark = getString(object, "remark");
		log.batchNo = getString(object, "batch_no");
		if (!isNull(object, QUERY_FEE)) {
			log.fee = getDouble(object, QUERY_FEE);
		}
		if (!isNull(object, QUERY_DESCRIPTION)) {
			log.description = getString(object, QUERY_DESCRIPTION);
		}
		if (object.containsKey(QUERY_SUCCESS_TIME) && !StringUtils.isEmpty(getString(object, QUERY_SUCCESS_TIME)) && !getString(object, QUERY_SUCCESS_TIME).equals("0000-00-00 00:00:00")) {
			log.successTime = CoreDateUtils.parseDate(getString(object, QUERY_SUCCESS_TIME), CoreDateUtils.DATETIME);
		}
		if (object.containsKey("remit_channel")) {
			log.remitChannel = RemitChannel.getItem(getInt(object, "remit_channel"));
		}
		if (object.containsKey("real_fee")) {
			log.realFee = getDouble(object, "real_fee");
		}
		if (object.containsKey("real_fee_status")) {
			log.realFeeStatus = YesNoStatus.getItem(getInt(object, "real_fee_status"));
		}
		if (object.containsKey("refund_type")) {
			log.refundType = WithdrawRefundManualType.getItem(getInt(object, "refund_type"));
		}
		return log;
	}
	
	public static List<WithdrawLog> convertFromJSONArray(JSONArray array) {
		if (array == null) {
			return null;
		}
		List<WithdrawLog> list = new ArrayList<WithdrawLog>();
		for (Iterator<?> iterator = array.iterator(); iterator.hasNext();) {
			JSONObject object = (JSONObject) iterator.next();
			list.add(convertFromJSONObject(object));
		}
		return list;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public long getUid() {
		return uid;
	}

	public void setUid(long uid) {
		this.uid = uid;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public Date getTimeline() {
		return timeline;
	}

	public void setTimeline(Date timeline) {
		this.timeline = timeline;
	}

	public double getAmount() {
		return amount;
	}

	public void setAmount(double amount) {
		this.amount = amount;
	}

	public WithdrawStatus getWithdrawStatus() {
		return withdrawStatus;
	}

	public void setWithdrawStatus(WithdrawStatus withdrawStatus) {
		this.withdrawStatus = withdrawStatus;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getIdData() {
		return idData;
	}

	public void setIdData(String idData) {
		this.idData = idData;
	}

	public BankType getBankType() {
		return bankType;
	}

	public void setBankType(BankType bankType) {
		this.bankType = bankType;
	}

	public String getBankRealname() {
		return bankRealname;
	}

	public void setBankRealname(String bankRealname) {
		this.bankRealname = bankRealname;
	}

	public String getBankBranch() {
		return bankBranch;
	}

	public void setBankBranch(String bankBranch) {
		this.bankBranch = bankBranch;
	}

	public String getBankCardno() {
		return bankCardno;
	}

	public void setBankCardno(String bankCardno) {
		this.bankCardno = bankCardno;
	}

	public WithdrawType getWithdrawType() {
		return withdrawType;
	}

	public void setWithdrawType(WithdrawType withdrawType) {
		this.withdrawType = withdrawType;
	}

	public YesNoStatus getExportStatus() {
		return exportStatus;
	}

	public void setExportStatus(YesNoStatus exportStatus) {
		this.exportStatus = exportStatus;
	}

	public String getFrozenId() {
		return frozenId;
	}

	public void setFrozenId(String frozenId) {
		this.frozenId = frozenId;
	}

	public String getExtra() {
		return extra;
	}

	public void setExtra(String extra) {
		this.extra = extra;
	}
	
	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public String getBankId() {
		return bankId;
	}

	public void setBankId(String bankId) {
		this.bankId = bankId;
	}

	public String getBatchNo() {
		return batchNo;
	}

	public void setBatchNo(String batchNo) {
		this.batchNo = batchNo;
	}

	public int getProvinceId() {
		return provinceId;
	}

	public void setProvinceId(int provinceId) {
		this.provinceId = provinceId;
	}

	public int getCityId() {
		return cityId;
	}

	public void setCityId(int cityId) {
		this.cityId = cityId;
	}

	public String getProvinceName() {
		return provinceName;
	}

	public void setProvinceName(String provinceName) {
		this.provinceName = provinceName;
	}

	public String getCityName() {
		return cityName;
	}

	public void setCityName(String cityName) {
		this.cityName = cityName;
	}

	public WalletType getWalletType() {
		return walletType;
	}

	public void setWalletType(WalletType walletType) {
		this.walletType = walletType;
	}

	public void setFee(Double fee) {
		this.fee = fee;
	}

	public Double getFee() {
		return fee;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return description;
	}

	public void setSuccessTime(Date successTime) {
		this.successTime = successTime;
	}

	public Date getSuccessTime() {
		return successTime;
	}

	public void setBankName(String bankName) {
		this.bankName = bankName;
	}

	public String getBankName() {
		return bankName;
	}

	public void setWithdrawSerialNumber(String withdrawSerialNumber) {
		this.withdrawSerialNumber = withdrawSerialNumber;
	}

	public String getWithdrawSerialNumber() {
		return withdrawSerialNumber;
	}

	public void setMemberSerialNumber(String memberSerialNumber) {
		this.memberSerialNumber = memberSerialNumber;
	}

	public String getMemberSerialNumber() {
		return memberSerialNumber;
	}

	public void setRemitReturnStatus(YesNoStatus remitReturnStatus) {
		this.remitReturnStatus = remitReturnStatus;
	}

	public YesNoStatus getRemitReturnStatus() {
		return remitReturnStatus;
	}

	public void setResultStatus(YesNoStatus resultStatus) {
		this.resultStatus = resultStatus;
	}

	public YesNoStatus getResultStatus() {
		return resultStatus;
	}

	public void setResultStatusValue(Integer resultStatusValue) {
		this.resultStatusValue = resultStatusValue;
	}

	public Integer getResultStatusValue() {
		return resultStatusValue;
	}

	public void setConfirmFlag(boolean confirmFlag) {
		this.confirmFlag = confirmFlag;
	}

	public boolean isConfirmFlag() {
		return confirmFlag;
	}

	public void setExsitValue(String exsitValue) {
		this.exsitValue = exsitValue;
	}

	public String getExsitValue() {
		return exsitValue;
	}

	public void setImportValue(String importValue) {
		this.importValue = importValue;
	}

	public String getImportValue() {
		return importValue;
	}

	public void setComfirmMemo(String comfirmMemo) {
		this.comfirmMemo = comfirmMemo;
	}

	public String getComfirmMemo() {
		return comfirmMemo;
	}

	public RemitChannel getRemitChannel() {
		return remitChannel;
	}

	public void setRemitChannel(RemitChannel remitChannel) {
		this.remitChannel = remitChannel;
	}

	public double getRealFee() {
		return realFee;
	}

	public void setRealFee(double realFee) {
		this.realFee = realFee;
	}

	public YesNoStatus getRealFeeStatus() {
		return realFeeStatus;
	}

	public void setRealFeeStatus(YesNoStatus realFeeStatus) {
		this.realFeeStatus = realFeeStatus;
	}

    public String getAcqssn() {
        return acqssn;
    }

    public void setAcqssn(String acqssn) {
        this.acqssn = acqssn;
    }

    public String getTransDatetime() {
        return transDatetime;
    }

    public void setTransDatetime(String transDatetime) {
        this.transDatetime = transDatetime;
    }

	public WithdrawRefundManualType getRefundType() {
		return refundType;
	}

	public void setRefundType(WithdrawRefundManualType refundType) {
		this.refundType = refundType;
	}
}
